package com.cn.web.controller.mc;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.bc.User;
import com.cn.bean.pojo.mc.K3FlXx;
import com.cn.bean.pojo.mc.McHcList;
import com.cn.bean.pojo.mc.McYc;
import com.cn.bean.pojo.mc.McYcList;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db1.mc.McYcDao;
import com.cn.service.server.bc.MaterialServer;
import com.cn.service.server.mc.McYcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class McYcController {
    @Autowired(required = false)
    private McYcService mcYcService;
    @Autowired(required = false)
    private MaterialServer materialServer;

    @RequestMapping("/MC/get/K3FlXx/All")
    @ResponseBody
    public List<K3FlXx> getK3FlXxAll(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String djBh = jsonObject.getString("djBh");
        String k3Mc = jsonObject.getString("k3Mc");
        String parentId = jsonObject.getString("parentId");
        djBh = (djBh != null) ? djBh : "";
        k3Mc = (k3Mc != null) ? k3Mc : "";
        parentId = (parentId != null) ? parentId : "";
        List<K3FlXx> k3FlXxAll = mcYcService.getK3FlXxAll(limit, page, djBh,k3Mc, parentId);
        return k3FlXxAll;
    }
    @RequestMapping("/MC/add/McYc")
    @ResponseBody
    public Result addMcYc(@RequestBody JSONObject jsonObject) {
        Integer zt = jsonObject.getInteger("zt");
        String currentFormattedDateTime = jsonObject.getString("currentFormattedDateTime");
        currentFormattedDateTime = currentFormattedDateTime.replace("/", "-");
        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        McYc mcYc=new McYc();
        mcYc.setSqrGh(userName);
        mcYc.setSqrXm(name);
        mcYc.setSqrBm(bmmc);
        mcYc.setZt(5);
        mcYc.setSqSj(currentFormattedDateTime);
        Integer id = mcYcService.getAutoidMcYc();
        if (id != null) {
            id++;
        } else {
            id = 1;
        }
        String documentNumber = String.valueOf(id);
        mcYc.setDocumentNumber(documentNumber);
        mcYc.setId(id);
        mcYc.setDjsj(currentFormattedDateTime);
        int k = mcYcService.addMcYc(mcYc);
        Boolean isOK = true;
        JSONArray formDataList = jsonObject.getJSONArray("formDataList");
        if (k==0){
            isOK = false;
        }
        if (!formDataList.isEmpty()) {
            for (int i = 0; i < formDataList.size(); i++) {
                JSONObject formData = formDataList.getJSONObject(i);
                McYcList mcYcList = formData.toJavaObject(McYcList.class);
                Integer idList = mcYcService.getAutoidMcYcList();
                if (idList != null) {
                    idList++;
                } else {
                    idList = 1;
                }
                mcYcList.setIdList(idList);
                mcYcList.setParentId(id);
                int k1 = mcYcService.addMcYcList(mcYcList);
                int k2 = mcYcService.upDateMcK3FlDgXxZt(mcYcList.getDjBh(), 5);
                if (k1==0||k2==0){
                    isOK=false;
                }
            }
        }
         if (isOK){
             return Result.success("200","保存成功");
         }
         return Result.failure("500","保存失败");
    }
    @RequestMapping("/MC/get/Mc/Yc")
    @ResponseBody
    public List<McYc> getMcYc(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String documentNumber = jsonObject.getString("documentNumber");
        documentNumber = (documentNumber != null) ? documentNumber : "";
        List<McYc> mcYc = mcYcService.getMcYc(limit, page, documentNumber);
        return mcYc;
    }
}
